﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Snowdreamist.Helpers
{
    internal static class IOHelper
    {
        public const int BlockBufferSize = 1024;

        public static string ReadToNextChar(this TextReader reader, char endChar, long longestTestLength)
        {
            if (reader == null)
                throw new ArgumentNullException("reader");
            if (!(reader is StreamReader))
                throw new NotSupportedException("Only support StreamReader");
            StreamReader stmReader = reader as StreamReader;
            if (!stmReader.BaseStream.CanSeek)
                throw new NotSupportedException("BaseStream cannot seek");

            long recoverPosition = stmReader.BaseStream.Position;
            int currentLength = 0;
            bool isSuccess = false;
            char[] buffer = new char[BlockBufferSize];
            StringBuilder strBuilder = new StringBuilder();

            while (true)
            {
                if (currentLength >= longestTestLength)
                {
                    // end test break
                    break;
                }

                int size = stmReader.ReadBlock(buffer, 0, BlockBufferSize);

                if (size == 0)
                {
                    // EOF, end test
                    break;
                }

                // test
                string content = new string(buffer);
                var endCharIndex = content.IndexOf(endChar);
                if (endCharIndex != -1)
                {
                    // hit
                    isSuccess = true;
                    strBuilder.Append(content.Substring(0, endCharIndex));
                    break;
                }

                strBuilder.Append(content);

                if (size < BlockBufferSize)
                {
                    break;
                }

                // update length
                currentLength += size;
            }

            if (isSuccess)
            {
                string result = strBuilder.ToString();
                // restore to the endchar position
                stmReader.BaseStream.Seek(recoverPosition + result.Length, SeekOrigin.Begin);

                return result;
            }
            else
            {
                // restore to the recover position
                stmReader.BaseStream.Seek(recoverPosition, SeekOrigin.Begin);
            }

            return null;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="startChar"></param>
        /// <param name="endChar"></param>
        /// <param name="allowNest">Allow nest char eg: [aa[mm]bb]</param>
        /// <returns></returns>
        public static string ReadContentSurroundedWithCharPair(this TextReader reader, char startChar, char endChar, long maxTryLength, bool isAllowNest = false)
        {
            throw new NotImplementedException();
        }
    }
}
